package com.wedu.modules.dss.controller;

import com.wedu.common.utils.PageUtils;
import com.wedu.modules.dss.entity.DeviceInformationEntity;
import com.wedu.modules.dss.entity.MaintenanceWarningEntity;
import com.wedu.modules.dss.service.DeviceInformationService;
import com.wedu.modules.dss.service.MaintenanceWarningService;
import com.wedu.modules.sys.controller.AbstractController;
import com.wedu.common.utils.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("dss/maintenanceWarning")
public class MaintenanceWarningController extends AbstractController {

    @Autowired
    private MaintenanceWarningService maintenanceWarningService;

    @Autowired
    private DeviceInformationService deviceInformationService;

    /**
     * 查看*/
    @GetMapping("/list1")
    @RequiresPermissions("dss:warning:list1")
    public R list(@RequestParam Map<String,Object> map){
        PageUtils pageUtils = maintenanceWarningService.queryPageWarning(map);
        R r = R.ok();
        r.put("page",pageUtils);
        return r;
    }

    /**
     * 根据id查看*/
    @GetMapping("/info/{deviceId}")
    @RequiresPermissions("dss:warning:info")
    public R info(@PathVariable("deviceId") String deviceId){
//        MaintenanceWarningDTO maintenanceWarningDTO
        MaintenanceWarningEntity maintenanceWarningEntity = maintenanceWarningService.getDevice(deviceId);
        R r = R.ok();
        r.put("maintenanceWarning",maintenanceWarningEntity);
        return r;
    }

    /**
     * 添加*/
    @PostMapping("/save")
    @RequiresPermissions("dss:warning:save")
    public R save(@RequestBody MaintenanceWarningEntity maintenanceWarningEntity){
        maintenanceWarningEntity.setCreateTime(new Date());
        maintenanceWarningEntity.setCreateBy(getUserId());
        R r = maintenanceWarningService.saveWarning(maintenanceWarningEntity);
        return r;
    }

//    @PostMapping("/saveXY")
//    public String saveMaintenanceData(@RequestBody MaintenanceData maintenanceData) {
//        // 处理接收到的数据
//        System.out.println("保养周期 X: " + maintenanceData.getX());
//        System.out.println("提前提醒 Y: " + maintenanceData.getY());
//        // 这里可以添加业务逻辑，例如保存到数据库
//        return "数据接收成功！";
//    }
    /**
     * 更新*/
    @PostMapping("/update1")
    @RequiresPermissions("dss:warning:update1")
    public R update(@RequestBody MaintenanceWarningEntity maintenanceWarningEntity){
        maintenanceWarningEntity.setUpdateTime(new Date());
        maintenanceWarningEntity.setUpdateBy(getUserId());
        maintenanceWarningEntity.setMaintenanceStatus(1);
//        MaintenanceData maintenanceData = new MaintenanceData();/***/

        if (maintenanceWarningEntity.getMaintenancePlan().equals("1")){
            Integer x = maintenanceWarningEntity.getMaintenanceCycle();
            Integer y = maintenanceWarningEntity.getAdvanceTime();
            Date date1 = new Date();
            Calendar calendar1 = Calendar.getInstance();
            calendar1.add(Calendar.DATE,x);/***/
            date1 = calendar1.getTime();
            maintenanceWarningEntity.setMaintenanceDueTime(date1);
            Date date2 = new Date();
            Calendar calendar2 = Calendar.getInstance();
            calendar2.add(Calendar.DATE,(x-y));/***/
            date2 = calendar2.getTime();
            maintenanceWarningEntity.setNoticeTime(date2);
        }else if (maintenanceWarningEntity.getMaintenancePlan().equals("2")){
            Integer x = maintenanceWarningEntity.getMaintenanceCycle();
            Date date1 = new Date();
            Calendar calendar1 = Calendar.getInstance();
            calendar1.add(Calendar.HOUR,x);/***/
            date1 = calendar1.getTime();
            maintenanceWarningEntity.setMaintenanceDueTime(date1);
            maintenanceWarningEntity.setNoticeTime(date1);
        }
        maintenanceWarningService.updateWarning(maintenanceWarningEntity);
        return R.ok();
    }

    /**
     * 按钮：点击保养状态更新为保养中*/
    @PostMapping("/maintenance1")
    @RequiresPermissions("dss:warning:maintenance1")
    public R maintenance(@RequestBody String[] deviceId){

//        MaintenanceRecordsEntity maintenanceRecordsEntity = new MaintenanceRecordsEntity();
//        maintenanceRecordsEntity.setUserId();
        maintenanceWarningService.maintenance(deviceId,getUserId());
        return R.ok();
    }

    /**
     * 将设备编号与设备名称存入map
     * */
    @GetMapping("/deviceList")
    @RequiresPermissions("dss:warning:list")
    public R deviceList(){
        List<DeviceInformationEntity> deviceInformationEntityList = deviceInformationService.getdeviceName();
        List<Map<String,Object>> deviceList = new ArrayList<>();
        for (DeviceInformationEntity deviceInformationEntity : deviceInformationEntityList) {
            Map<String,Object> map = new HashMap<>();
            map.put("deviceId",deviceInformationEntity.getDeviceId());
            map.put("workshopName",deviceInformationEntity.getWorkshopName());
            map.put("deviceType",deviceInformationEntity.getDeviceType());
            map.put("deviceName",deviceInformationEntity.getDeviceName());
            map.put("deviceModel",deviceInformationEntity.getDeviceModel());
            deviceList.add(map);
        }

        R r = R.ok();
        r.put("deviceList",deviceList);
        return r;
    }


}

